package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.ReaderBean;
import com.xuxin.baby.contract.ReaderContract;
import com.xuxin.baby.database.DBControl;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.presenter.ReaderPresenter;
import com.xuxin.baby.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.21 0021
 * 文章详情
 */
public class ReaderActivity extends BaseActivity implements ReaderContract.View {
    private final ReaderContract.Presenter presenter = new ReaderPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.reader)
    LinearLayout reader;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.reader_title)
    TextView reader_title;
    @BindView(R.id.reader_image)
    ImageView reader_image;
    @BindView(R.id.reader_context)
    WebView reader_content;

    private String tenantId;//租户id
    private String token;//授权token
    private String readerId;//视频文章id

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_reader);
    }

    @Override
    public void initView() {
        title_name.setText("文章详情");
        reader_content.setHorizontalScrollBarEnabled(false);//水平不显示
        reader_content.setVerticalScrollBarEnabled(false); //垂直不显示
        reader_content.setWebViewClient(webViewClient);
        reader_content.setWebChromeClient(webChromeClient);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        readerId = getIntent().getStringExtra(BaseConstant.readerId);
        LogUtils.e("readerId = " + readerId);
        popup.setTitle("加载中...").show();
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
    }

    @Override
    @OnClick({R.id.title_left})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {//文章详情
                presenter.getReader(tenantId, token, readerId);
            }
            return false;
        }
    });

    @Override
    public void onSuccess(ReaderBean readerBean) {
        if (null != readerBean.getResult()) {
            DBControl.insertHistory(readerBean);
            reader_title.setText(readerBean.getResult().getTitle());
            String content = readerBean.getResult().getContents();
            reader_content.loadDataWithBaseURL(null, content,
                    "text/html;charset=utf-8", "UTF-8", null);
            String imageUrl = readerBean.getResult().getImageUrl();
            if (!StringUtils.isEmpty(imageUrl)) {
                try {
                    String image = Retrofit.image_url + imageUrl;
                    LogUtils.e("文章详情_image = " + image);
                    GlideApp.with(context).load(image).placeholder(R.mipmap.img_placeholder1440).centerCrop().into(reader_image);
                    reader_image.setVisibility(View.VISIBLE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    WebChromeClient webChromeClient = new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            LogUtils.e("newProgress = " + newProgress);
            popup.setTitle("加载中 " + newProgress + " %...");
        }
    };

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            LogUtils.e("onPageFinished");
            dismiss();
            reader.setVisibility(View.VISIBLE);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            LogUtils.e("onReceivedError");
            dismiss();
            reader.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            LogUtils.e("onReceivedError2");
            dismiss();
            reader.setVisibility(View.GONE);
        }
    };
}